package org.daidai.abstrusewriting.homepage.live.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.annotations.SerializedName;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.live.adapters.MyAlbumIdAdapter;
import org.daidai.abstrusewriting.homepage.live.models.AlbumId;
import org.daidai.abstrusewriting.homepage.live.models.AlbumIdListItem;
import org.daidai.abstrusewriting.homepage.live.models.DetailHeader;
import org.daidai.abstrusewriting.homepage.live.models.liveSmiple;
import org.daidai.abstrusewriting.homepage.live.networks.ClientNetwork;
import org.daidai.abstrusewriting.homepage.live.tools.CircleImage;
import org.daidai.abstrusewriting.homepage.live.tools.myView;
import org.daidai.abstrusewriting.homepage.top.PlayActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class LiveDetailActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private liveSmiple simple;
    private DetailHeader data;
    private ListView lv;
    private AlbumId albumId;
    private List<AlbumIdListItem> Items;
    private MyAlbumIdAdapter myAlbumIdAdapter;
    private ImageView viewById;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        EventBus.getDefault().register(this);

        Intent intent = getIntent();
        simple= intent.getParcelableExtra("data");
        ClientNetwork.getData(Long.toString(simple.getUid()));
        ClientNetwork.getAbomData(Long.toString(simple.getUid()));
        Toast.makeText(this, simple.getUid()+"", Toast.LENGTH_SHORT).show();
        lv = (ListView) findViewById(R.id.detil_list);
        Items =new ArrayList<>();
        myAlbumIdAdapter = new MyAlbumIdAdapter(this);
        lv.setOnItemClickListener(this);
       // initview();
//        ArrayList<String> list = new ArrayList<>();
//        for (int i = 0; i < 100; i++) {
//            list.add("文件"+i);
//        }
//        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,android.R.id.text1, list);

        lv.setAdapter(myAlbumIdAdapter);

    }

    @Override
    protected void onResume() {

        super.onResume();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveData(DetailHeader d)
    {
        data=d;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveAlbumIdData(AlbumId a)
    {
        albumId=a;
        Log.d("vvvvvv", "onCreate: "+albumId.getAlbumId());
        ClientNetwork.getAbulmItemDetail(Long.toString(albumId.getAlbumId()));
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getAlbumItem(List<AlbumIdListItem> l)
    {
        if (l != null) {
           AlbumIdListItem albumIdListItem = l.get(0);
            if (albumIdListItem instanceof AlbumIdListItem)
            {
                Items=l;
                initview();
                myAlbumIdAdapter.set(l);
                myAlbumIdAdapter.notifyDataSetChanged();
//                for (int i = 0; i < Items.size(); i++) {
//                    Log.d("eeeeeeeee", "receiveAlbumIdData: "+Items.get(i).getTitle());
//                }

            }
        }
    }

    private void initview() {
        View headerview = LayoutInflater.from(this).inflate(R.layout.detail_item_header, null);
        LinearLayout linearLayout = (LinearLayout) headerview.findViewById(R.id.linear);
        ImageView v= (ImageView) headerview.findViewById(R.id.detail_header_iv);
        v.setOnClickListener(this);
        ImageView vv= (ImageView) headerview.findViewById(R.id.detail_header_iv_2);
        vv.setOnClickListener(this);
        ImageView vvv= (ImageView) headerview.findViewById(R.id.detail_header_iv_1);
        vvv.setOnClickListener(this);
        Picasso.with(linearLayout.getContext())
                .load(data.getBackgroundLogo())
                .config(Bitmap.Config.RGB_565)
                .into(new myView(linearLayout));
        ImageView iv = (ImageView) headerview.findViewById(R.id.detail_header_name_iv);
        Picasso.with(iv.getContext())
                .load(simple.getSmallLogo())
                .resize(120,120)
                .config(Bitmap.Config.RGB_565)
                .into(iv);
        TextView tv_name = (TextView) headerview.findViewById(R.id.name);
        tv_name.setText(data.getNickname());
        TextView tv_jian = (TextView) headerview.findViewById(R.id.detail_jian);
        tv_jian.setText(data.getPtitle());
        TextView guan_1= (TextView) headerview.findViewById(R.id.detail_tv_1);
        guan_1.setText(Long.toString(data.getFollowings()));
        TextView guan_2= (TextView) headerview.findViewById(R.id.detail_tv_2);
        guan_2.setText(Long.toString(data.getFollowers()));
        TextView guan_3= (TextView) headerview.findViewById(R.id.detail_tv_3);
        guan_3.setText(Long.toString(data.getFavorites()));
        /*--------------------------------*/
        ImageView ss= (ImageView) headerview.findViewById(R.id.ji_iv);
        Picasso.with(ss.getContext())
                .load(albumId.getCoverSmall())
                .config(Bitmap.Config.RGB_565)
                .into(ss);

        TextView tv_d= (TextView) headerview.findViewById(R.id.tv_d);
        tv_d.setText(albumId.getTitle());
        TextView textView = (TextView) headerview.findViewById(R.id.tv_t);
        textView.setText(albumId.getPlayTimes()+"");
        TextView viewById = (TextView) headerview.findViewById(R.id.tv_ff);
        viewById.setText(albumId.getTracks()+"");
        Button bt = (Button) headerview.findViewById(R.id.bt);
        bt.setText("发布的声音("+albumId.getTracks()+")");
        lv.addHeaderView(headerview);
        Log.d("ssss", "receiveData: "+data.getBackgroundLogo());

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.detail_header_iv:
                finish();
                break;
            case R.id.detail_header_iv_2:
                ShareSDK.initSDK(this);
                OnekeyShare oks = new OnekeyShare();
                //关闭sso授权
                oks.disableSSOWhenAuthorize();
                    // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
                    //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
                    // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
                    oks.setTitle("标题");
                    // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
                    oks.setTitleUrl("http://sharesdk.cn");
                    // text是分享文本，所有平台都需要这个字段
                    oks.setText("我是分享文本");
                    //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
                    oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
                    // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
                    //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
                    // url仅在微信（包括好友和朋友圈）中使用
                    oks.setUrl("http://sharesdk.cn");
                // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                oks.setComment("我是测试评论文本");
                // site是分享此内容的网站名称，仅在QQ空间使用
                oks.setSite("ShareSDK");
                // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                oks.setSiteUrl("http://sharesdk.cn");
                // 启动分享GUI
                oks.show(this);
                break;
        }


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        AlbumIdListItem albumIdListItem=null;
        if (Items.size()==1)
        {
           albumIdListItem = Items.get(0);
        }
        else {
            albumIdListItem = Items.get(i);
        }
//        Log.d("oooooooooo00", "trckeid: "+albumIdListItem.getTrackId());
//        Log.d("oooooooooo00", "uid: "+albumIdListItem.getUid());
//
//        Toast.makeText(this,albumIdListItem.getLikes()+"", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, PlayActivity.class);
        long uid =albumIdListItem.getUid();
        long trackId = albumIdListItem.getTrackId();
        intent.putExtra("uid" , uid);
        intent.putExtra("trackId" , trackId);
        startActivity(intent);

    }
}
